//
//  ZBButton.m
//  ZBTechniqueLibrary
//
//  Created by apple on 2018/9/29.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ZBButton.h"
@interface ZBButton()

@property (nonatomic, assign)   ZBButtonType btnType;
@end

@implementation ZBButton
+ (ZBButton *)zb_buttonWithType:(ZBButtonType)buttonType
{
    return [self zb_buttonWithType:buttonType offset:0];
}
+ (ZBButton *)zb_buttonWithType:(ZBButtonType)buttonType offset:(CGFloat)offset
{
    ZBButton *button = [ZBButton buttonWithType:UIButtonTypeCustom];
    button.btnType = buttonType;
    button.offset = offset;
    return button;
}
- (void)setOffset:(CGFloat)offset
{
    _offset = offset;
    [self layoutIfNeeded];
    [self setNeedsLayout];
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    [self layoutIfNeeded];
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 按钮的宽度/高度
     CGFloat    buttonWidth = self.bounds.size.width;
  //   CGFloat    buttonHeight = self.bounds.size.height;
    
    //标题的宽度高度
    CGFloat    titleWidth = ceil([self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].width);
    CGFloat    titleHeight = ceil([self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].height);
    
    CGFloat    imageWidth = self.imageView.image.size.width;
    CGFloat    imageHeight = self.imageView.image.size.height;
    switch (self.btnType) {
        case ZBButtonType_None:
            {
                
            }
            break;
        case ZBButtonType_LeftImageRightText:
            {
                CGFloat space = self.offset;
                if(titleWidth > buttonWidth - imageWidth - space){
                    titleWidth = buttonWidth - imageWidth - space;
                }
                [self setImageEdgeInsets:UIEdgeInsetsMake(0, -space*0.5, 0, space*0.5)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(0, space*0.5, 0, -space*0.5)];
            }
            break;
        case ZBButtonType_LeftTextRightImage:
            {
                CGFloat space = self.offset;
                if(titleWidth > buttonWidth - imageWidth - space){
                    titleWidth = buttonWidth - imageWidth - space;
                }
                [self setImageEdgeInsets:UIEdgeInsetsMake(0, (titleWidth + space*0.5), 0, -(titleWidth + space*0.5))];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imageWidth+space*0.5), 0, (imageWidth+space*0.5))];
                
            }
            break;
        case ZBButtonType_TopImageBottomText:
            {
                CGFloat space = self.offset;
                if(titleWidth > buttonWidth){
                    titleWidth = buttonWidth;
                }
                CGFloat imageLeft = (titleWidth+imageWidth)*0.5-imageWidth*0.5;
                CGFloat titleRight = (titleWidth+imageWidth)*0.5-titleWidth*0.5;
                [self setImageEdgeInsets:UIEdgeInsetsMake(-(imageHeight*0.5 + space*0.5), imageLeft, imageHeight*0.5 + space*0.5, -imageLeft)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(titleHeight *0.5+space*0.5, -titleRight, -(titleHeight *0.5+space*0.5), titleRight)];
            }
            break;
        case ZBButtonType_TopTextBottomImage:
            {
                CGFloat space = self.offset;
                if(titleWidth > buttonWidth){
                    titleWidth = buttonWidth;
                }
               
                CGFloat imageLeft = (titleWidth+imageWidth)*0.5-imageWidth*0.5;
                CGFloat titleRight = (titleWidth+imageWidth)*0.5-titleWidth*0.5;
                [self setImageEdgeInsets:UIEdgeInsetsMake(titleHeight *0.5+space*0.5, imageLeft, -(titleHeight *0.5+space*0.5), -imageLeft)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(-(imageHeight *0.5+space*0.5), -titleRight, imageHeight *0.5+space*0.5, titleRight)];
            }
            break;
            
        default:
            break;
    }
    
}
@end
